# -*- coding: UTF-8 -*-
import requests
import time
import sys
import os
from bs4 import BeautifulSoup
from pypinyin import Style, lazy_pinyin

ROOT_URL = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/"
HEADER = {
    "User-Agent": "Mozilla/6.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/66.0"
}


def prase_county(url):
    time.sleep(0.5)
    url = ROOT_URL + url
    response = requests.get(url, headers=HEADER)
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.text, 'lxml')
    county_container_list = soup.find_all('tr', class_='countytr')
    for county_container in county_container_list:
        county_list = county_container.find_all('a')
        if len(county_list) < 2:
            continue

        county = county_list[1]
        name = county.text
        print("    ", name)


def prase_city(url):
    time.sleep(0.5)
    url = ROOT_URL + url
    response = requests.get(url, headers=HEADER)
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.text, 'lxml')
    city_container_list = soup.find_all('tr', class_='citytr')
    index = 1
    for city_container in city_container_list:
        city_list = city_container.find_all('a')
        city = city_list[1]
        url = city['href']
        name = city.text
        print("  ", name)
        prase_county(url)
        if index > 2:
            return
        index = index + 1


def parse_provincetr():
    response = requests.get(ROOT_URL, headers=HEADER)
    response.encoding = response.apparent_encoding
    soup = BeautifulSoup(response.text, 'lxml')
    province_container_list = soup.find_all('tr', class_='provincetr')

    index = 1
    for province_container in province_container_list:
        province_list = province_container.find_all('a')
        for province in province_list:
            url = province['href']
            name = province.text
            pinyin_full = lazy_pinyin(name)
            pinyin_h = lazy_pinyin(name, style=Style.INITIALS, strict=False)
            # print("province url = ", url)
            print("================")
            print(name)
            print("pin=", pinyin_full)
            print("pinyin_h=", pinyin_h)
            prase_city(url)
            if index > 2:
                return
            index = index + 1


if __name__ == '__main__':
    parse_provincetr()
